<?php
/**
 * 建设周报控制器
 * @author ZhouYP
 **/
namespace Admin\Controller;
use Common\Controller\WahahabaseController;

class ConstructionWeeklyReportController extends WahahabaseController{

	public function _initialize() {
		parent::_initialize();
		$this->deal_model = D("Common/ConstructionWeeklyReport");
	}
	 
	 
	public function index(){
			/**搜索条件**/
		$where = array("d.is_deleted"=>array('neq', 1));
		$this->getDepartLevel();
		$this->indexPage($where);

	}
	/**
     *  添加
     */
	public function plan_add(){
		$first_day_of_week = date('Y-m-d',mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y')));
		$end_day_of_week = date('Y-m-d',mktime(0,0,0,date('m'),date('d')+(7-date('w')),date('Y')));
		$this->assign('first', $first_day_of_week);
		$this->assign('end', $end_day_of_week);
		$this->display();
	}
    public function plan_add_post(){
		if(IS_POST){
			// var_dump($_POST);exit();
			// 合并底层数据
			$data = array_merge($_POST, $this->saveData());
			// var_dump($data);exit();
			if ($this->deal_model->create()!==false) {
				$result = $this->deal_model->add($data);
				if ($result!==false) {
					$this->success("添加成功！", U("ConstructionWeeklyReport/index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->deal_model->getError());
			}
		}
	}	
	/**
	 *  修改
	 */
	public function edit(){
		$id = I('get.id',0,'intval');
		$data = $this->deal_model->where(array("id"=>$id))->find();
		$this->assign("data", $data);
		if ($data[record_type]==1) {
			$this->display('plan_edit');
		}else{
			$this->display('complete_edit');
		}
	}

	public function  view(){
		 $this->getDepartLevel();
		 $id = I('get.id',0,'intval');
		 $data=$this->deal_model->where(array("id"=>$id))->find();
		 $this->assign("data",$data);
		 if ($data[record_type]==1) {
			$this->display('plan_view');
		 }else{
			$this->display('complete_view');
		 }
	}

	public function plan_edit_post(){
		if (IS_POST) {
			$data = array_merge($_POST, $this->saveData(2));
			if ($this->deal_model->create()!==false) {
				$result=$this->deal_model->save($data);
				if ($result !== false) {
					$this->success("保存成功！", U("ConstructionWeeklyReport/index"));
				} else {
					$this->error("保存失败！");
				}
			} else {
				$this->error($this->deal_model->getError());
			}
		}
	}
	/**
	 *  添加
	 */
	public function complete_add(){
		$first_day_of_week = date('Y-m-d',mktime(0,0,0,date('m'),date('d')-date('w')+1,date('Y')));
		$end_day_of_week = date('Y-m-d',mktime(0,0,0,date('m'),date('d')+(7-date('w')),date('Y')));
		$this->assign('first', $first_day_of_week);
		$this->assign('end', $end_day_of_week);
		$this->display();
	}
	public function complete_add_post(){
		if(IS_POST){
			// 合并底层数据
			$data = array_merge($_POST, $this->saveData());
			// var_dump($data);exit();
			if ($this->deal_model->create()!==false) {
				$result = $this->deal_model->add($data);
				if ($result!==false) {
					$this->success("添加成功！", U("ConstructionWeeklyReport/index"));
				} else {
					$this->error("添加失败！");
				}
			} else {
				$this->error($this->deal_model->getError());
			}
		}
	}
	/**
	 *  修改
	 */
	public function complete_edit(){
		$this->display();
	}

	        public function complete_edit_post(){
	           // var_dump($_POST);exit();
			if (IS_POST) {
				$data = array_merge($_POST, $this->saveData(2));
				if ($this->deal_model->create()!==false) {
					$result=$this->deal_model->save($data);
					if ($result !== false) {
						$this->success("保存成功！", U("ConstructionWeeklyReport/index"));
					} else {
						$this->error("保存失败！");
					}
				} else {
					$this->error($this->deal_model->getError());
					}
				}
		}

	public function delete(){
	    $id = I('get.id', 0, 'intval');
		if(!$id){
			$this->error("请选择要删除的数据！");
		}
		$data = array(
			'id'	=> $id,
			'is_deleted' => 1
		);
		if ($this->deal_model->save($data) !== false) {
			$this->success("删除成功！");
		} else {
			$this->error("删除失败！");
		}
	}
	###########################################
	
	/*下载成word的方法*/

	public function in_word(){
		$id = I('post.id', 0, 'intval');
		$data = $this->deal_model->where(array("id"=>$id))->find();
		//计划
		$data_plan = $this->deal_model->where(array("being_date"=>$data['being_date'],"record_type"=>1))->find();
		//完成情况
		$data_completed= $this->deal_model->where(array("being_date"=>$data['being_date'],"record_type"=>2))->find();
		//var_dump($data_completed);exit();
		$html = "<table width='550' border='1' style='border-color:black;border-collapse:collapse' cellpadding='2'>\n"
		        ."<tr>\n"
		        ."<th style='text-align:center;font-size:20px;width:120px; height:50px' colspan='2'>日期</th>\n"
		        ."<th style='text-align:center;font-size:20px;' width='200'>计 划</th>\n"
		        ."<th style='text-align:center;font-size:20px;' width='200'>完成情况</th>\n"
		        ."</tr>\n"
		        ."<tr>\n"
		        ."<th rowspan='2'>周一</th>\n"
		        ."<td>上午</td>\n"
		        ."<td>".$data_plan["monday_am"]."</td>\n"
		        ."<td>".$data_completed["monday_am"]."</td>\n"
		        ."</tr>\n"
		        ."<tr>\n"
		        ."<td>下午</td>\n"
		        ."<td>".$data_plan["monday_pm"]."</td>\n"
		        ."<td>".$data_completed["monday_pm"]."</td>\n"
		        ."</tr>\n"
		        ."<th rowspan='2'>周二</th>\n"
		        ."<td>上午</td>\n"
		        ."<td>".$data_plan["tuesday_am"]."</td>\n"
		        ."<td>".$data_completed["tuesday_am"]."</td>\n"
		        ."</tr>\n"
		        ."<tr>\n"
		        ."<td>下午</td>\n"
		        ."<td>".$data_plan["tuesday_pm"]."</td>\n"
		        ."<td>".$data_completed["tuesday_pm"]."</td>\n"
		        ."</tr>\n"
		        ."<th rowspan='2'>周三</th>\n"
		        ."<td>上午</td>\n"
		        ."<td>".$data_plan["wednesday_am"]."</td>\n"
		        ."<td>".$data_completed["wednesday_am"]."</td>\n"
		        ."</tr>\n"
		        ."<tr>\n"
		        ."<td>下午</td>\n"
		        ."<td>".$data_plan["wednesday_pm"]."</td>\n"
		        ."<td>".$data_completed["wednesday_pm"]."</td>\n"
		        ."</tr>\n"
		        ."<th rowspan='2'>周四</th>\n"
		        ."<td>上午</td>\n"
		        ."<td>".$data_plan["thursday_am"]."</td>\n"
		        ."<td>".$data_completed["thursday_am"]."</td>\n"
		        ."</tr>\n"
		        ."<tr>\n"
		        ."<td>下午</td>\n"
		        ."<td>".$data_plan["thursday_pm"]."</td>\n"
		        ."<td>".$data_completed["thursday_pm"]."</td>\n"
		        ."</tr>\n"
		        ."<th rowspan='2'>周五</th>\n"
		        ."<td>上午</td>\n"
		        ."<td>".$data_plan["friday_am"]."</td>\n"
		        ."<td>".$data_completed["friday_am"]."</td>\n"
		        ."</tr>\n"
		        ."<tr>\n"
		        ."<td>下午</td>\n"
		        ."<td>".$data_plan["friday_pm"]."</td>\n"
		        ."<td>".$data_completed["friday_pm"]."</td>\n"
		        ."</tr>\n"
		         ."<th rowspan='2'>周六</th>\n"
		        ."<td>上午</td>\n"
		        ."<td>".$data_plan["saturday_am"]."</td>\n"
		        ."<td>".$data_completed["saturday_am"]."</td>\n"
		        ."</tr>\n"
		        ."<tr>\n"
		        ."<td>下午</td>\n"
		        ."<td>".$data_plan["saturday_pm"]."</td>\n"
		        ."<td>".$data_completed["saturday_pm"]."</td>\n"
		        ."</tr>\n"
		        ."<th rowspan='2'>周天</th>\n"
		        ."<td>上午</td>\n"
		        ."<td>".$data_plan["sunday_am"]."</td>\n"
		        ."<td>".$data_completed["sunday_am"]."</td>\n"
		        ."</tr>\n"
		        ."<tr>\n"
		        ."<td>下午</td>\n"
		        ."<td>".$data_plan["sunday_pm"]."</td>\n"
		        ."<td>".$data_completed["sunday_pm"]."</td>\n"
		        ."</tr>\n"
		        ."<th rowspan='2'>备注</th>\n"
		        ."<td rowspan='2' colspan='3'></td>\n"
		        ."</tr>\n";
		$time = time();
		$title = "$time";
		$filepath=$this->cword($html,iconv("UTF-8","GB2312//IGNORE",$title));
		exit(json_encode(array(
                'rtnPath'	=> "$filepath",
                'rtnName'	=> "$title".".doc"
            )));
	}
	/*生成word文件*/
	public function cword($data,$fileName=''){

	    if(empty($data)) return '';
	  
	    $data = '<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">'.$data.'</html>';
	    $dir  = "./docfile/".date("Ymd")."/";
	  
	    if(!file_exists($dir)) mkdir($dir,777,true);
	  
	    if(empty($fileName))
	    {
	        $fileName=$dir.date('His').'.doc';
	    }
	    else
	    {
	        $fileName =$dir.$fileName.'.doc';
	    }
	  
	    $writefile = fopen($fileName,'wb') or die("创建文件失败"); //wb以二进制写入
	    fwrite($writefile,$data);
	    fclose($writefile);

	    return $fileName;
	}

	#######################接口############################
	##工作计划接口
	public function getWeekReportAjax(){
		$info = $this->deal_model
				  ->alias("d")
				  ->field('d.*,u.real_name')
				  ->join(array(
                  ' LEFT JOIN '.C('DB_PREFIX').'users u ON d.add_user = u.id',
             	  ))

				 ->where(array('d.is_publish' => 1, 'd.is_deleted' => 0))
            	 ->order("d.add_time DESC")
				 ->limit(5)
				 ->select();
		exit(json_encode(
			array(
				'rtnType' => 'T',
				'rtnData' => $info
			)
		));		
	}
	
}



?>